Print Archive

Requirement:

Sending the files (.doc, .docx, .xls, .xlsx, .pdf, .txt, .csv) to print archive runtime in sync or async or batch mode (X++, D365FO, AX 2012)

Solution:

public static void savePrintArchive(System.IO.Stream _stream, str _fileName, str contentType, str fileContentType)
{
    DocuRef                docuRef;
    PrintJobHeader         printJobHeader;

    ttsbegin;
    printJobHeader.format = PrintFormat::PDF;
    printJobHeader.jobDescription = _fileName;
    printJobHeader.jobStatus = PrintJobStatus::Completed;
    printJobHeader.jobType = contentType;
    printJobHeader.printedBy = curuserid();
    printJobHeader.printedDate = DateTimeUtil::date(DateTimeUtil::getSystemDateTime());
    printJobHeader.printedTime = DateTimeUtil::time(DateTimeUtil::getSystemDateTime());
    printJobHeader.numberOfPages = 0; // we can't get number of pages from PDF without loading it into the memory.
    printJobHeader.doInsert();
    ttscommit;

    str attachmentName = System.IO.Path::GetFileNameWithoutExtension(_fileName);

    if (_stream.CanSeek)
    {
        _stream.Seek(0, System.IO.SeekOrigin::Begin);
    }

    docuRef = DocumentManagement::attachFile(
        tableNum(PrintJobHeader),
        printJobHeader.RecId,
        curExt(),
        DocuType::typeFile(),
        _stream,
        _fileName,
        fileContentType,
        attachmentName
    );
}